<?php
/**
 * Created by PhpStorm.
 * Clerk: yunpeng
 * Date: 2018/12/11
 * Time: 16:17
 */

namespace app\admin\controller;

use app\admin\Controller;
use think\config;
use think\Db;
use think\Exception;
use think\exception\HttpException;

class Count extends Controller
{
    use \app\admin\traits\controller\Controller;
    protected static $isdelete = false;

    public function index(){
        $model = new \app\admin\model\Client();

        // 列表过滤器，生成查询Map对象
        $map = $this->search($model);
        if ($this::$isdelete !== false) {
            $map['isdelete'] = $this::$isdelete;
        } else {
            $map['isdelete'] = 0;
        }

        // 自定义过滤器
        if (method_exists($this, 'filter')) {
            $this->filter($map);
        }

        $listRows = $this->request->param('numPerPage') ?: Config::get("paginate.list_rows");

        // 排序字段 默认为主键名
        $order = $this->request->param('_order') ?: (empty($sortBy) ? $model->getPk() : $sortBy);
        // 接受 sort参数 0 表示倒序 非0都 表示正序
        $asc = false;
        $sort = null !== $this->request->param('_sort') ?
            ($this->request->param('_sort') == 'asc' ? 'asc' : 'desc') :
            ($asc ? 'asc' : 'desc');



        $client_num = Db::name('client')->where($map)->count('*');
        $clerk_num = Db::name('clerk')->where($map)->count('*');
        $today = \Time::today();
        $week = \Time::week();
        $month = \Time::month();
        $year = \Time::year();
        $today_money = \app\admin\model\Order::getOrderMoney($today);
        $week_money = \app\admin\model\Order::getOrderMoney($month);
        $year_money = \app\admin\model\Order::getOrderMoney($year);

        $cycle = $this->request->param('cycle') ? $this->request->param('cycle') : 'week';
        if($cycle == 'week'){
            $time =$week;
        }elseif($cycle == 'month'){
            $time = $month;
        }elseif($cycle == 'year'){
            $time = $year;
        }else{
            return ajax_return_adv_error('周期错误');
        }

        $list = Db::name('clerk')
            //->fetchSql(true)
            ->alias('a')
            ->field('a.id,a.name,sum(b.order_money) as money')
            ->join('order b','a.id = b.clerk_id')
            ->where('a.isdelete','0')
            ->where('a.create_time','>',$time[0])
            ->where('a.create_time','<=',$time[1])
            ->group('a.id')
            ->order('money',$sort)
            ->paginate($listRows, false, ['query' => $this->request->get()]);

        $_sort = ($sort == 'asc') ? 'desc' : 'asc';
        $this->view->assign('sort', $_sort);
        $this->view->assign('client_num', $client_num);
        $this->view->assign('clerk_num', $clerk_num);
        $this->view->assign('today_money', $today_money);
        $this->view->assign('week_money', $week_money);
        $this->view->assign('year_money', $year_money);
        $this->view->assign('list', $list);
        $this->view->assign("count", $list->total());
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());

        return $this->view->fetch();

    }

}